(in-package :at.etel.ws.sql.test)

(defun initialize ()
  (unless *metadata*
    (warn "No database metadata in ws-sql::*metadata* special variable.")
    (database-initialize)
    (possibly-refresh-metadata)))

(define-test serialize-procedures
  (let ((database-id "at.etel.jdbc.infodb.test")
	(procedure-pattern "WEB_PASSWD_CHG_OCCURRED")
	(schema-pattern "EUIDB"))
    (verbose "Serializing procedures from '~A'" database-id)
    (initialize)
    (verbose "which match SCHEMA:PATTERN ~A:~A" schema-pattern procedure-pattern)
    (let ((procedures 
	   (get-procedures   
	    :procedure-name-pattern procedure-pattern
	    :schema-pattern schema-pattern)))
      (assert-true (> (length procedure-pattern) 0))
      (loop :for procedure :in procedures 
	 :do (serialize-procedure-description (list procedure))))))

(define-test transcribe-wsdl
  (verbose "running test-wsdl")
  (initialize)
  (let ((procedures (get-procedures   
		     :catalog "PASSWD_CHANGE" 
		     :procedure-name-pattern "WEB_PASSWD_CHG_OCCURRED"
		     :schema-pattern "EUIDB")))
    (multiple-value-bind (wsdl xsd)
	(transcribe-wsdl procedures)
      (assert-true (hash-table-p wsdl))
      (assert-true (hash-table-p xsd))
      (assert-true (> (hash-table-count wsdl) 0))
      (assert-true (> (hash-table-count xsd) 0)))))

(define-test persist-transcribed-procedure
  (let* ((wsdl-path #p"test.wsdl")
	 (xsd-path #p "test.xsd"))
    (initialize) 
    (multiple-value-bind (wsdl-lxml xsd-lxml)
	(transcribe-wsdl-as-xml 
	 (get-procedures 
	  :schema-pattern "EUIDB"
	  :procedure-name-pattern "WEB_PASSWD_CHG_OCCURRED"))
      (persist-lxml wsdl-lxml wsdl-path)
      (assert-true (probe-file wsdl-path))
      (persist-lxml xsd-lxml xsd-path)
      (assert-true (probe-file xsd-path)))))
    
	   


